<template>
  <div class="alarm-rule-list-container">
    <header-search-form
      :searchFormData="searchForm"
      @search="search"
    />
    <lan-table
      size="small"
      :config="dataTable"
      :tableData="tableData"
      @search="search"
    >
      <template #type="scope">
        <el-input v-model="scope.row.type"></el-input>
      </template>
      <template #active="scope">
        <span
          v-if="scope.row.active"
          style="color: #07c160;"
        >启用</span>
        <span
          v-else
          style="color: #fa5151;"
        >不启用</span>
      </template>
    </lan-table>
    <new-edit
      ref="editComponent"
      @search="search"
    />
  </div>
</template>

<script>
import lanTable from '../../../components/lanTable';
import newEdit from './newEdit';
import HeaderSearchForm from './headerSearchForm.vue';

import * as moment from 'moment';
import { Message, MessageBox } from 'element-ui';
import { SortType, MomentFormat } from '../../../constants/common';
import { parseError } from '../../../utils';
import { getRouterListApi, deleteRouterApi } from '../../../api/router';
import { getRouterTypeDisplay } from './servers';
export default {
  components: {
    lanTable,
    newEdit,
    HeaderSearchForm
  },
  data() {
    return {
      searchForm: {
        btnLoading: false,
        type: '',
        name: '',
        description: '',
        active: ''
      },
      sort: {
        orderBy: 'createdAt',
        sortType: SortType.DESC
      },
      tableData: [],
      dataTable: {
        pageSize: 10,
        totalCount: 0,
        currentPage: 1,
        pageSizes: [5, 10, 20, 50, 100],
        sort: {
          orderBy: 'createdAt',
          sortType: SortType.DESC
        },
        columnList: [
          {
            label: '',
            type: 'index'
          },
          {
            label: '广播类型',
            name: 'type',
            scope: true,
            type: 'formatter',
            width: '120px',
            formatter: getRouterTypeDisplay
          },
          {
            name: 'name',
            label: '名称'
          },
          {
            name: 'description',
            label: '描述'
          },
          {
            name: 'active',
            label: '开关状态',
            type: 'slot'
          },
          {
            name: 'createdAt',
            label: '创建时间',
            sortable: true
          },
          {
            name: 'updatedAt',
            label: '更新时间'
          },
          {
            type: 'button',
            label: '操作',
            buttons: [
              {
                type: 'text',
                text: '设备映射',
                size: 'small',
                method: row => {
                  this.deviceCast(row);
                }
              },
              {
                type: 'text',
                text: '编辑',
                size: 'small',
                method: row => {
                  this.showEdit(row);
                }
              },
              {
                type: 'text',
                text: '删除',
                size: 'small',
                method: row => {
                  this.remove(row.uuid, row.name);
                }
              }
            ]
          }
        ]
      }
    };
  },
  created() {
    this.search();
  },
  methods: {
    getRouterTypeDisplay,
    search() {
      this.searchForm.btnLoading = true;
      getRouterListApi(
        this.dataTable.currentPage - 1,
        this.dataTable.pageSize,
        this.dataTable.sort.sortType,
        this.dataTable.sort.orderBy,
        this.searchForm.type,
        this.searchForm.name,
        this.searchForm.description,
        this.searchForm.active
      )
        .then(({ total_count, data }) => {
          this.dataTable.totalCount = total_count;
          this.tableData = data.map(router => {
            return {
              uuid: router.uuid,
              type: router.type,
              name: router.name,
              description: router.description,
              active: router.active,
              senderDeviceList: router.sender_device_list,
              receiverDeviceList: router.receiver_device_list,
              createdAt: moment(router.created_at).format(MomentFormat),
              updatedAt: moment(router.updated_at).format(MomentFormat)
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.searchForm.btnLoading = false));
    },
    showEdit(row) {
      this.$refs.editComponent.showEditDialog(row);
    },
    remove(uuid, name) {
      MessageBox.confirm(`确认删除路由规则${name}吗？`, '删除提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        deleteRouterApi(uuid)
          .then(() => Message.success('删除路由规则成功'))
          .catch(error => Message.error(parseError(error)))
          .finally(() => this.search());
      });
    },
    deviceCast(row) {
      const deviceCast = Object.assign({
        uuid: row.uuid,
        receiverDeviceList: row.receiverDeviceList,
        senderDeviceList: row.senderDeviceList
      });
      this.$store.dispatch('setDeviceCastObject', deviceCast);
      this.$router.push({
        name: 'deviceCast',
        query: {
          type: row.type,
          name: row.name
        }
      });
    },
    pageSizeChangeHandler() {
      this.currentPage = 1;
      this.search();
    },
    currentPageChangeHandler() {
      this.search();
    }
  }
};
</script>

<style lang="scss">
.alarm-rule-list-container {
  padding: 10px;
}
</style>
